package cn.jsc.basic.util;

import cn.jsc.user.domain.LoginInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * 封装获取登陆上下文的信息
 */
public class LoginContextUtil {
    public static LoginInfo getLoginInfo(HttpServletRequest request){
        //获取上下文对象
        ServletContext context = request.getServletContext();
        //获取Sring容器对象
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        //从Spring容器中获取redisTemplate对象
        RedisTemplate redisTemplate = applicationContext.getBean("redisTemplate", RedisTemplate.class);
        //获取请求头里面的token
        String token = request.getHeader("token");
        //获取readis里面的loginInfo对象
        Object obj = redisTemplate.opsForValue().get(token);
        if(obj!=null){//登陆过
            return (LoginInfo) obj;
        }
        return null;

    }

}
